/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jiadakeji.ppm.modules.business.web;

import com.jiadakeji.ppm.common.config.Global;
import com.jiadakeji.ppm.common.persistence.Page;
import com.jiadakeji.ppm.common.utils.StringUtils;
import com.jiadakeji.ppm.common.web.BaseController;
import com.jiadakeji.ppm.modules.business.entity.TaxRate;
import com.jiadakeji.ppm.modules.business.service.TaxRateService;
import com.jiadakeji.ppm.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 税率Controller
 * @author HHAOYU
 * @version 2019-08-19
 */
@Controller
@RequestMapping(value = "${adminPath}/business/taxRate")
public class TaxRateController extends BaseController {

	@Autowired
	private TaxRateService taxRateService;
	
	@ModelAttribute
	public TaxRate get(@RequestParam(required=false) String id) {
		TaxRate entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = taxRateService.get(id);
		}
		if (entity == null){
			entity = new TaxRate();
		}
		return entity;
	}
	
	@RequiresPermissions("business:taxRate:view")
	@RequestMapping(value = {"list", ""})
	public String list(TaxRate taxRate, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TaxRate> page = taxRateService.findPage(new Page<TaxRate>(request, response), taxRate); 
		model.addAttribute("page", page);
		
		UserUtils.putCache("taxRate", taxRate);
		return "modules/business/taxRateList";
	}

	@RequiresPermissions("business:taxRate:view")
	@RequestMapping(value = "form")
	public String form(TaxRate taxRate, Model model) {
		model.addAttribute("taxRate", taxRate);
		return "modules/business/taxRateForm";
	}

	@RequiresPermissions("business:taxRate:edit")
	@RequestMapping(value = "save")
	public String save(TaxRate taxRate, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, taxRate)){
			return form(taxRate, model);
		}
		taxRateService.save(taxRate);
		addMessage(redirectAttributes, "保存税率成功");
		
		redirectAttributes.addFlashAttribute("taxRate", UserUtils.getCache("taxRate"));
		return "redirect:"+Global.getAdminPath()+"/business/taxRate/?repage";
	}
	
	@RequiresPermissions("business:taxRate:edit")
	@RequestMapping(value = "delete")
	public String delete(TaxRate taxRate, RedirectAttributes redirectAttributes) {
		taxRateService.delete(taxRate);
		addMessage(redirectAttributes, "删除税率成功");
		
		redirectAttributes.addFlashAttribute("taxRate", UserUtils.getCache("taxRate"));
		return "redirect:"+Global.getAdminPath()+"/business/taxRate/?repage";
	}

}